package br.com.online.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import br.com.online.modelo.Empresa;


public class EmpresaDao {
	
	private final Session session;

	public EmpresaDao(Session session) {
		this.session = session;
	}

	public void salva(Empresa empresa) {

		Transaction tx = session.beginTransaction();
		session.save(empresa);
		tx.commit();
	}
	
	@SuppressWarnings("unchecked")
	public List<Empresa> listaTudo() {
		return this.session.createCriteria(Empresa.class).list();
	}

	public Empresa carrega(Long id) {
		return (Empresa) this.session.load(Empresa.class, id);
	}

	public void atualiza(Empresa empresa) {
		Transaction tx = session.beginTransaction();
		session.update(empresa);
		tx.commit();
	}

	public void remove(Empresa empresa) {
		Transaction tx = session.beginTransaction();
		session.delete(empresa);
		tx.commit();
	}

	@SuppressWarnings("unchecked")
	public List<Empresa> busca(String nome) {
		return session.createCriteria(Empresa.class)
				.add(Restrictions.ilike("nome", nome, MatchMode.ANYWHERE))
				.list();
	}

	public void recarrega(Empresa empresa) {
		session.refresh(empresa);
	}

}
